﻿/*******************************************************************************
* file  : rsa key text
* creat : apleilx
* data  : 2017/03/02
*******************************************************************************/
#include "common.h"
#include "crc_ext.h"
#include "mbedtls/rsa.h"
#include "mbedtls/entropy.h"
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/platform.h"
#include "bsp_api.h"
#include "stdio_cfg.h"

extern void * rsa_src, *rsa_enc, *rsa_dec;

#define DEV_RANDOM_THRESHOLD        32
int rsa_random_entropy_poll( void *, unsigned char *, size_t, size_t *);
const char seed_src[] = "rsa ce shi";

/*******************************************************************************
* @brief  priv dec.
* \param[in] none
* \retval: none
*******************************************************************************/
void ras_priv_dec(void)
{
	int ret = 1;
	FILE* f;
	
	mbedtls_rsa_context *rsa_obj;
	mbedtls_mpi *mpi;
	mbedtls_mpi *N, *P, *Q, *D, *E;
    mbedtls_entropy_context *entropy;
    mbedtls_ctr_drbg_context *ctr_drbg;
	
	// 内存请求
	rsa_obj = malloc(sizeof(mbedtls_rsa_context));
	mpi = calloc(8,sizeof(mbedtls_mpi));
	entropy = malloc(sizeof(mbedtls_entropy_context));
	ctr_drbg = malloc(sizeof(mbedtls_ctr_drbg_context));
	
	// 组分配
	N = &mpi[0];
	P = &mpi[1];
	Q = &mpi[2];
	D = &mpi[3];
	E = &mpi[4];

	
	dbg_print("\nAES 128 encode test: <128 bits> \n");
	
	// 取一段原始数据
	memcpy(rsa_src, &sinx_0r5pi_2048_16384[0], 1024);
	
	// 初始化结构
	mbedtls_rsa_init(rsa_obj);
    mbedtls_ctr_drbg_init( ctr_drbg );
    mbedtls_entropy_init( entropy );
    mbedtls_mpi_init( N ); mbedtls_mpi_init( P ); mbedtls_mpi_init( Q );
    mbedtls_mpi_init( D ); mbedtls_mpi_init( E ); 
	
	mbedtls_rsa_set_padding(rsa_obj, MBEDTLS_RSA_PKCS_V21, MBEDTLS_MD_SHA256);
	
	// 随机数源
	ret = mbedtls_entropy_add_source( 
		entropy, 
		rsa_random_entropy_poll,
		NULL, 
		DEV_RANDOM_THRESHOLD,
		MBEDTLS_ENTROPY_SOURCE_STRONG );
	
    if(ret)
	{
        mbedtls_printf( " failed\n  ! mbedtls_entropy_add_source returned -0x%04x\n", (unsigned int) -ret );
		goto rsa_code_exit;
    }
	
	// 生成随机数
    ret = mbedtls_ctr_drbg_seed( 
		ctr_drbg, mbedtls_entropy_func,
		entropy, 
		(const unsigned char *) seed_src,
        strlen( seed_src ) );
	
    if(ret)
    {
        dbg_print( " failed\n  ! mbedtls_ctr_drbg_seed returned %d\n", ret );
		goto rsa_code_exit;
    }
	
	// 读取私匙
    if( ( f = fopen( "rsa_priv_txt", "rb" ) ) == NULL )
    {
        dbg_print( " failed\n  ! Could not open rsa_priv.txt\n" \
                "  ! Please run rsa_genkey first\n\n" );
		goto rsa_code_exit;
    }
	

    fclose( f );

	// 输入私匙
    if( ( ret = mbedtls_rsa_import( rsa_obj, N, P, Q, D, E ) ) != 0 )
    {
        dbg_print( " failed\n  ! mbedtls_rsa_import returned %d\n\n", ret );
        goto rsa_code_exit;
    }

	// 设置完成
    if( ( ret = mbedtls_rsa_complete( rsa_obj ) ) != 0 )
    {
        dbg_print( " failed\n  ! mbedtls_rsa_complete returned %d\n\n", ret );
        goto rsa_code_exit;
    }
	
	// 加密
	/*
    ret = mbedtls_rsa_pkcs1_decrypt( rsa_obj, mbedtls_ctr_drbg_random,
                                            ctr_drbg, &len_back,
                                            rsa_enc, rsa_dec, 1024 );
	*/

	// 释放，用于清除内存中的密匙，属于安全性代码
	mbedtls_rsa_free(rsa_obj);

rsa_code_exit:
	// 释放内存
	free(rsa_obj);
	free(mpi);
	free(entropy);
	free(ctr_drbg);
}
